package com.wanmait.jd.task;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanmait.jd.pojo.Item;
import com.wanmait.jd.service.ItemService;
import com.wanmait.jd.utils.HttpUtils;
import org.apache.http.client.methods.HttpGet;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ItemTask {

    @Autowired
    private HttpUtils httpUtils;
    @Autowired
    private ItemService itemService;

    private static  final ObjectMapper MAPPER = new ObjectMapper();



    //当下载任务完成后多长时间进行下一次下载任务
    @Scheduled(fixedDelay =  100*1000)
    public  void ItemTask() throws Exception {
        //声明需要解析的初始地址
        String url = "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&wq=%E6%89%8B%E6%9C%BA&s=117&click=0&page=";
        //按照页码对手机的搜索结果进行遍历
        for (int i = 1; i <10 ; i=i+2) {
            String html = httpUtils.doGet(url + i);

            //解析页面，获取商品数据并存取
            this.parse(html);
        }
        System.out.println("手机数据抓取成功");
    }

    private void parse(String html) throws Exception {
        //解析html，获取Document对象
        Document document = Jsoup.parse(html);

        //获取spu
        Elements elements = document.select("div#J_goodsList > ul > li");
        for (Element element : elements) {
            //获取spu
            long spu = 0;
            if(element.attr("data-spu")!="")
            {
                spu = Long.parseLong(element.attr("data-spu"));
                System.out.println(spu);
            }

            //获取sku
            if(elements.select("li.ps-item")!=null)
            {
                Elements skuEles = elements.select("li.ps-item");
                for (Element skuEle : skuEles) {
                    long sku = Long.parseLong(skuEle.select("[data-sku]").attr("data-sku"));
                    //根据sku查询商品数据，
                    Item item = new Item();
                    item.setSku(sku);
                    List<Item> list = this.itemService.findll(item);
                    if(list.size()>0){
                        // 如果商品存在就进行下一次的查询
                        continue;
                    }
                    //设置商品的spu
                    item.setSpu(spu);

                    //获取商品详情的url
                    String itemurl = "https://item.jd.com/"+sku+".html";
                    item.setUrl(itemurl);

                    //获取商品的图片
                    String picUrl = "http:"+skuEle.select("img[data-sku]").first().attr("data-lazy-img");
                    picUrl = picUrl.replace("/n9/","/n1/");
                    String picName = this.httpUtils.doGetImagera(picUrl);
                    item.setPic(picName);

                    //获取商品的价格
                    String price = this.httpUtils.doGet("http://p.3.cn/prices/mgets?skuIds=J_" + sku);
                    System.out.println("商品价格"+price);
                    double prices= MAPPER.readTree(price).get(0).get("p").asDouble();
                    item.setPrice(prices);
                    //获取商品的标题
                    String itemInfo = this.httpUtils.doGet(itemurl);
                    String title = Jsoup.parse(itemInfo).select("div.sku-name").text();
                    item.setTitle(title);
                    //设置商品的创建时间
                    item.setCreated(new Date());
                    item.setUpdated(item.getCreated());
                    //保存数据数据库中、
                    this.itemService.save(item);
                }
            }


        }
    }

}
